package guowenlong.golf.ui.home;

import static android.os.Process.killProcess;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import guowenlong.golf.C;
import guowenlong.golf.R;
import guowenlong.golf.ui.dynamic.DynamicFragment;
import guowenlong.golf.ui.friend.FriendFragment;
import guowenlong.golf.ui.friendlymatch.FriendlyMatchActivity;
import guowenlong.golf.ui.login.LoginActivity;
import guowenlong.golf.ui.news.NewsFragment;
import guowenlong.golf.ui.personal.PersonalFragment;
import guowenlong.golf.widget.AddDialog;
import guowenlong.golf.widget.AddDialog.OnClickListener;
import me.wenlong.mixutils.mvp.BaseActivity;
import me.wenlong.mixutils.util.SpUtil;


public class HomeActivity extends BaseActivity<HomePresenter, HomeModel> implements
    HomeContract.View {

  @BindView(R.id.fl_fragment)
  FrameLayout flFragment;
  @BindView(R.id.tab_events)
  RadioButton tabEvents;
  @BindView(R.id.tab_dynamic)
  RadioButton tabDynamic;
  @BindView(R.id.tab_friend)
  RadioButton tabFriend;
  @BindView(R.id.tab_persona)
  RadioButton tabPersona;
  @BindView(R.id.tab_radioGroup)
  RadioGroup tabRadioGroup;
  @BindView(R.id.tab_add)
  TextView tabAdd;
  private int page_number = C.FRAGMENT_NEWS;
  private NewsFragment mNewsFragment;
  private DynamicFragment mDynamicFragment;
  private FriendFragment mFriendFragment;
  private PersonalFragment mPersonalFragment;

  @Override
  protected int getContentView(Bundle savedInstanceState) {
    return R.layout.activity_main;
  }

  @Override
  protected void initView(Bundle savedInstanceState) {
    getSwipeBackLayout().setEnablePullToBack(false);
    selectFragment(page_number);
  }

  @Override
  protected void initData() {

  }

  @Override
  protected void onResume() {
    super.onResume();
    if (SpUtil.getUser() == null) {
      selectFragment(C.FRAGMENT_DYNAMIC);
    }
  }

  @OnClick({R.id.tab_events, R.id.tab_dynamic, R.id.tab_friend, R.id.tab_persona, R.id.tab_add})
  public void onClick(View view) {
    if (view.getId() != R.id.tab_dynamic) {
      if (SpUtil.getUser() == null) {
        startActivityForResult(new Intent(HomeActivity.this, LoginActivity.class), C.ACTIVITY_HOME);
      }
    }
    switch (view.getId()) {
      case R.id.tab_events:
        selectFragment(C.FRAGMENT_NEWS);
        break;
      case R.id.tab_dynamic:
        selectFragment(C.FRAGMENT_DYNAMIC);
        break;
      case R.id.tab_friend:
        selectFragment(C.FRAGMENT_FRIEND);
        break;
      case R.id.tab_persona:
        selectFragment(C.FRAGMENT_PERSONAL);
        break;
      case R.id.tab_add:
        AddDialog addDialog = new AddDialog(this, R.style.MyDialog, new OnClickListener() {
          @Override
          public void onClickListener() {
            mContext.startActivity(new Intent(mContext, FriendlyMatchActivity.class));
          }
        });
        addDialog.show();
        break;
    }
  }

  // ----------------------------------- fragment -----------------------------------------------//

  public void selectFragment(int fragmentNumber) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    hideFragments(transaction);
    switch (fragmentNumber) {
      case C.FRAGMENT_NEWS:
        tabEvents.setChecked(true);
        if (mNewsFragment == null) {
          mNewsFragment = new NewsFragment();
          transaction.add(R.id.fl_fragment, mNewsFragment);
        }
        transaction.show(mNewsFragment);
        break;
      case C.FRAGMENT_DYNAMIC:
        tabDynamic.setChecked(true);
        if (mDynamicFragment == null) {
          mDynamicFragment = new DynamicFragment();
          transaction.add(R.id.fl_fragment, mDynamicFragment);
        }
        transaction.show(mDynamicFragment);
        break;
      case C.FRAGMENT_FRIEND:
        tabFriend.setChecked(true);
        if (mFriendFragment == null) {
          mFriendFragment = new FriendFragment();
          transaction.add(R.id.fl_fragment, mFriendFragment);
        }
        transaction.show(mFriendFragment);
        break;
      case C.FRAGMENT_PERSONAL:
        tabPersona.setChecked(true);
        if (mPersonalFragment == null) {
          mPersonalFragment = new PersonalFragment();
          transaction.add(R.id.fl_fragment, mPersonalFragment);
        }
        transaction.show(mPersonalFragment);
        break;
      default:
        break;
    }
    page_number = fragmentNumber;
    transaction.commit();
  }

  public FragmentTransaction hideFragments(FragmentTransaction transaction) {
    if (mNewsFragment != null) {
      transaction.hide(mNewsFragment);
    }
    if (mDynamicFragment != null) {
      transaction.hide(mDynamicFragment);
    }
    if (mFriendFragment != null) {
      transaction.hide(mFriendFragment);
    }
    if (mPersonalFragment != null) {
      transaction.hide(mPersonalFragment);
    }
    return transaction;
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    killProcess(android.os.Process.myPid());
  }
}